{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 7.10 \u5e26\u989d\u5916\u72b6\u6001\u4fe1\u606f\u7684\u56de\u8c03\u51fd\u6570\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u7684\u4ee3\u7801\u4e2d\u9700\u8981\u4f9d\u8d56\u5230\u56de\u8c03\u51fd\u6570\u7684\u4f7f\u7528(\u6bd4\u5982\u4e8b\u4ef6\u5904\u7406\u5668\u3001\u7b49\u5f85\u540e\u53f0\u4efb\u52a1\u5b8c\u6210\u540e\u7684\u56de\u8c03\u7b49)\uff0c\n\u5e76\u4e14\u4f60\u8fd8\u9700\u8981\u8ba9\u56de\u8c03\u51fd\u6570\u62e5\u6709\u989d\u5916\u7684\u72b6\u6001\u503c\uff0c\u4ee5\u4fbf\u5728\u5b83\u7684\u5185\u90e8\u4f7f\u7528\u5230\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e00\u5c0f\u8282\u4e3b\u8981\u8ba8\u8bba\u7684\u662f\u90a3\u4e9b\u51fa\u73b0\u5728\u5f88\u591a\u51fd\u6570\u5e93\u548c\u6846\u67b6\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u7684\u4f7f\u7528\u2014\u2014\u7279\u522b\u662f\u8ddf\u5f02\u6b65\u5904\u7406\u6709\u5173\u7684\u3002\n\u4e3a\u4e86\u6f14\u793a\u4e0e\u6d4b\u8bd5\uff0c\u6211\u4eec\u5148\u5b9a\u4e49\u5982\u4e0b\u4e00\u4e2a\u9700\u8981\u8c03\u7528\u56de\u8c03\u51fd\u6570\u7684\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def apply_async(func, args, *, callback):\n    # Compute the result\n    result = func(*args)\n\n    # Invoke the callback with the result\n    callback(result)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9e\u9645\u4e0a\uff0c\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u505a\u4efb\u4f55\u66f4\u9ad8\u7ea7\u7684\u5904\u7406\uff0c\u5305\u62ec\u7ebf\u7a0b\u3001\u8fdb\u7a0b\u548c\u5b9a\u65f6\u5668\uff0c\u4f46\u662f\u8fd9\u4e9b\u90fd\u4e0d\u662f\u6211\u4eec\u8981\u5173\u5fc3\u7684\u3002\n\u6211\u4eec\u4ec5\u4ec5\u53ea\u9700\u8981\u5173\u6ce8\u56de\u8c03\u51fd\u6570\u7684\u8c03\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6f14\u793a\u600e\u6837\u4f7f\u7528\u4e0a\u8ff0\u4ee3\u7801\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def print_result(result):\n    print('Got:', result)\ndef add(x, y):\n    return x + y\napply_async(add, (2, 3), callback=print_result)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "apply_async(add, ('hello', 'world'), callback=print_result)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6ce8\u610f\u5230 print_result() \u51fd\u6570\u4ec5\u4ec5\u53ea\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 result \u3002\u4e0d\u80fd\u518d\u4f20\u5165\u5176\u4ed6\u4fe1\u606f\u3002\n\u800c\u5f53\u4f60\u60f3\u8ba9\u56de\u8c03\u51fd\u6570\u8bbf\u95ee\u5176\u4ed6\u53d8\u91cf\u6216\u8005\u7279\u5b9a\u73af\u5883\u7684\u53d8\u91cf\u503c\u7684\u65f6\u5019\u5c31\u4f1a\u9047\u5230\u9ebb\u70e6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u8ba9\u56de\u8c03\u51fd\u6570\u8bbf\u95ee\u5916\u90e8\u4fe1\u606f\uff0c\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u4e00\u4e2a\u7ed1\u5b9a\u65b9\u6cd5\u6765\u4ee3\u66ff\u4e00\u4e2a\u7b80\u5355\u51fd\u6570\u3002\n\u6bd4\u5982\uff0c\u4e0b\u9762\u8fd9\u4e2a\u7c7b\u4f1a\u4fdd\u5b58\u4e00\u4e2a\u5185\u90e8\u5e8f\u5217\u53f7\uff0c\u6bcf\u6b21\u63a5\u6536\u5230\u4e00\u4e2a result \u7684\u65f6\u5019\u5e8f\u5217\u53f7\u52a01\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class ResultHandler:\n\n    def __init__(self):\n        self.sequence = 0\n\n    def handler(self, result):\n        self.sequence += 1\n        print('[{}] Got: {}'.format(self.sequence, result))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u7684\u65f6\u5019\uff0c\u4f60\u5148\u521b\u5efa\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u7528\u5b83\u7684 handler() \u7ed1\u5b9a\u65b9\u6cd5\u6765\u505a\u4e3a\u56de\u8c03\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "r = ResultHandler()\napply_async(add, (2, 3), callback=r.handler)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "apply_async(add, ('hello', 'world'), callback=r.handler)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7b2c\u4e8c\u79cd\u65b9\u5f0f\uff0c\u4f5c\u4e3a\u7c7b\u7684\u66ff\u4ee3\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u95ed\u5305\u6355\u83b7\u72b6\u6001\u503c\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def make_handler():\n    sequence = 0\n    def handler(result):\n        nonlocal sequence\n        sequence += 1\n        print('[{}] Got: {}'.format(sequence, result))\n    return handler"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4f7f\u7528\u95ed\u5305\u65b9\u5f0f\u7684\u4e00\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "handler = make_handler()\napply_async(add, (2, 3), callback=handler)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "apply_async(add, ('hello', 'world'), callback=handler)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u66f4\u9ad8\u7ea7\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u534f\u7a0b\u6765\u5b8c\u6210\u540c\u6837\u7684\u4e8b\u60c5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def make_handler():\n    sequence = 0\n    while True:\n        result = yield\n        sequence += 1\n        print('[{}] Got: {}'.format(sequence, result))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u534f\u7a0b\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u5b83\u7684 send() \u65b9\u6cd5\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "handler = make_handler()\nnext(handler) # Advance to the yield\napply_async(add, (2, 3), callback=handler.send)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "apply_async(add, ('hello', 'world'), callback=handler.send)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u57fa\u4e8e\u56de\u8c03\u51fd\u6570\u7684\u8f6f\u4ef6\u901a\u5e38\u90fd\u6709\u53ef\u80fd\u53d8\u5f97\u975e\u5e38\u590d\u6742\u3002\u4e00\u90e8\u5206\u539f\u56e0\u662f\u56de\u8c03\u51fd\u6570\u901a\u5e38\u4f1a\u8ddf\u8bf7\u6c42\u6267\u884c\u4ee3\u7801\u65ad\u5f00\u3002\n\u56e0\u6b64\uff0c\u8bf7\u6c42\u6267\u884c\u548c\u5904\u7406\u7ed3\u679c\u4e4b\u95f4\u7684\u6267\u884c\u73af\u5883\u5b9e\u9645\u4e0a\u5df2\u7ecf\u4e22\u5931\u4e86\u3002\u5982\u679c\u4f60\u60f3\u8ba9\u56de\u8c03\u51fd\u6570\u8fde\u7eed\u6267\u884c\u591a\u6b65\u64cd\u4f5c\uff0c\n\u90a3\u4f60\u5c31\u5fc5\u987b\u53bb\u89e3\u51b3\u5982\u4f55\u4fdd\u5b58\u548c\u6062\u590d\u76f8\u5173\u7684\u72b6\u6001\u4fe1\u606f\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u81f3\u5c11\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u5f0f\u6765\u6355\u83b7\u548c\u4fdd\u5b58\u72b6\u6001\u4fe1\u606f\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b(\u901a\u8fc7\u4e00\u4e2a\u7ed1\u5b9a\u65b9\u6cd5)\u6216\u8005\u5728\u4e00\u4e2a\u95ed\u5305\u4e2d\u4fdd\u5b58\u5b83\u3002\n\u4e24\u79cd\u65b9\u5f0f\u76f8\u6bd4\uff0c\u95ed\u5305\u6216\u8bb8\u662f\u66f4\u52a0\u8f7b\u91cf\u7ea7\u548c\u81ea\u7136\u4e00\u70b9\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u5f88\u7b80\u5355\u7684\u901a\u8fc7\u51fd\u6570\u6765\u6784\u9020\u3002\n\u5b83\u4eec\u8fd8\u80fd\u81ea\u52a8\u6355\u83b7\u6240\u6709\u88ab\u4f7f\u7528\u5230\u7684\u53d8\u91cf\u3002\u56e0\u6b64\uff0c\u4f60\u65e0\u9700\u53bb\u62c5\u5fc3\u5982\u4f55\u53bb\u5b58\u50a8\u989d\u5916\u7684\u72b6\u6001\u4fe1\u606f(\u4ee3\u7801\u4e2d\u81ea\u52a8\u5224\u5b9a)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f7f\u7528\u95ed\u5305\uff0c\u4f60\u9700\u8981\u6ce8\u610f\u5bf9\u90a3\u4e9b\u53ef\u4fee\u6539\u53d8\u91cf\u7684\u64cd\u4f5c\u3002\u5728\u4e0a\u9762\u7684\u65b9\u6848\u4e2d\uff0c\nnonlocal \u58f0\u660e\u8bed\u53e5\u7528\u6765\u6307\u793a\u63a5\u4e0b\u6765\u7684\u53d8\u91cf\u4f1a\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u88ab\u4fee\u6539\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\u58f0\u660e\uff0c\u4ee3\u7801\u4f1a\u62a5\u9519\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u800c\u4f7f\u7528\u4e00\u4e2a\u534f\u7a0b\u6765\u4f5c\u4e3a\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u5c31\u66f4\u6709\u8da3\u4e86\uff0c\u5b83\u8ddf\u95ed\u5305\u65b9\u6cd5\u5bc6\u5207\u76f8\u5173\u3002\n\u67d0\u79cd\u610f\u4e49\u4e0a\u6765\u8bb2\uff0c\u5b83\u663e\u5f97\u66f4\u52a0\u7b80\u6d01\uff0c\u56e0\u4e3a\u603b\u5171\u5c31\u4e00\u4e2a\u51fd\u6570\u800c\u5df2\u3002\n\u5e76\u4e14\uff0c\u4f60\u53ef\u4ee5\u5f88\u81ea\u7531\u7684\u4fee\u6539\u53d8\u91cf\u800c\u65e0\u9700\u53bb\u4f7f\u7528 nonlocal \u58f0\u660e\u3002\n\u8fd9\u79cd\u65b9\u5f0f\u552f\u4e00\u7f3a\u70b9\u5c31\u662f\u76f8\u5bf9\u4e8e\u5176\u4ed6Python\u6280\u672f\u800c\u8a00\u6216\u8bb8\u6bd4\u8f83\u96be\u4ee5\u7406\u89e3\u3002\n\u53e6\u5916\u8fd8\u6709\u4e00\u4e9b\u6bd4\u8f83\u96be\u61c2\u7684\u90e8\u5206\uff0c\u6bd4\u5982\u4f7f\u7528\u4e4b\u524d\u9700\u8981\u8c03\u7528 next() \uff0c\u5b9e\u9645\u4f7f\u7528\u65f6\u8fd9\u4e2a\u6b65\u9aa4\u5f88\u5bb9\u6613\u88ab\u5fd8\u8bb0\u3002\n\u5c3d\u7ba1\u5982\u6b64\uff0c\u534f\u7a0b\u8fd8\u6709\u5176\u4ed6\u7528\u5904\uff0c\u6bd4\u5982\u4f5c\u4e3a\u4e00\u4e2a\u5185\u8054\u56de\u8c03\u51fd\u6570\u7684\u5b9a\u4e49(\u4e0b\u4e00\u8282\u4f1a\u8bb2\u5230)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u4ec5\u4ec5\u53ea\u9700\u8981\u7ed9\u56de\u8c03\u51fd\u6570\u4f20\u9012\u989d\u5916\u7684\u503c\u7684\u8bdd\uff0c\u8fd8\u6709\u4e00\u79cd\u4f7f\u7528 partial() \u7684\u65b9\u5f0f\u4e5f\u5f88\u6709\u7528\u3002\n\u5728\u6ca1\u6709\u4f7f\u7528 partial() \u7684\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u7ecf\u5e38\u770b\u5230\u4e0b\u9762\u8fd9\u79cd\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u7684\u590d\u6742\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "apply_async(add, (2, 3), callback=lambda r: handler(r, seq))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u53c2\u80037.8\u5c0f\u8282\u7684\u51e0\u4e2a\u793a\u4f8b\uff0c\u6559\u4f60\u5982\u4f55\u4f7f\u7528 partial() \u6765\u66f4\u6539\u53c2\u6570\u7b7e\u540d\u6765\u7b80\u5316\u4e0a\u8ff0\u4ee3\u7801\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}